package controller

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/wonderivan/logger"
	"net/http"
	"teresa/service"
)

var Ingress ingress

type ingress struct{}

func (i *ingress) GetIngresses(ctx *gin.Context) {
	params := new(struct {
		Namespace  string `form:"namespace"`
		Limit      int    `form:"limit"`
		Page       int    `form:"page"`
		FilterName string `form:"filtername"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("Bind参数绑定失败," + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "Bind参数绑定失败," + err.Error(),
			"data": nil,
		})
		return
	}
	data, err := service.Ingress.GetIngresses(params.FilterName, params.Namespace, params.Limit, params.Page)
	if err != nil {
		logger.Error(err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取Ingress列表成功",
		"data": data,
	})
}
func (i *ingress) GetIngressDetail(ctx *gin.Context) {
	params := new(struct {
		Namespace   string `form:"namespace"`
		IngressName string `form:"ingressname"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("Bind绑定参数失败" + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "bind绑定参数失败" + err.Error(),
			"data": nil,
		})
		return
	}
	data, err := service.Ingress.GetIngressDetail(params.Namespace, params.IngressName)
	if err != nil {
		logger.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err,
			"data": nil,
		})
		return
	}
	var JsonIngressData Jsoningressdata
	jsoningressData, err := json.Marshal(data)
	if err != nil {
		fmt.Println("数据转换失败",err.Error())
		return
	}
	err = json.Unmarshal(jsoningressData, &JsonIngressData)
	if err != nil {
		fmt.Println("数据加载失败",err.Error())
		return
	}
	JsonIngressData.Metadata.ManagedFields = nil
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取ingress成功",
		"data": JsonIngressData,
	})
}

func (i *ingress) CreateIngress(ctx *gin.Context) {
	var (
		ingresscreate = new(service.IngressCreate)
		err           error
	)
	if err = ctx.ShouldBindJSON(ingresscreate); err != nil {
		logger.Error(errors.New("Bind参数绑定失败," + err.Error()))
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  errors.New("Bind参数绑定失败," + err.Error()),
			"data": nil,
		})
		return
	}
	err = service.Ingress.CreateIngress(ingresscreate)
	if err != nil {
		logger.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err,
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "创建ingress成功",
		"data": nil,
	})
}

func (i *ingress) DeleteIngress(ctx *gin.Context) {
	params := new(struct {
		Namespace string `form:"namespace"`
		Name      string `form:"name"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error(errors.New("bind绑定失败," + err.Error()))
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  errors.New("bind绑定失败," + err.Error()),
			"data": nil,
		})
		return
	}
	err := service.Ingress.DeleteIngress(params.Namespace, params.Name)
	if err != nil {
		logger.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err,
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "Ingress删除成功",
		"data": nil,
	})
}

func (i *ingress) UpdateIngress(ctx *gin.Context) {
	params := new(struct {
		Namespace string `json:"namespace"`
		Content   string `json:"content"`
	})
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error(errors.New("bind绑定失败," + err.Error()))
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  errors.New("bind绑定失败," + err.Error()),
			"data": nil,
		})
		return
	}
	err := service.Ingress.UpdateIngress(params.Namespace, params.Content)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "更新Ingress成功",
		"data": nil,
	})
}
